var express=require("express");
var http=express();
var fs=require("fs");
var query=require("querystring");
http.listen(8080,function () {
   console.log("服务器正在运行");
});
http.use(function (req,res,next) {
   res.header("Access-Control-Allow-Origin","*");
   next();
});
/*验证手机号*/
http.get("/blurTel",function (req,res) {
   var tel=req.query.tel;
   var bol=true;
   fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
      var arr=JSON.parse(data);
      for(var i=0;i<arr.length;i++){
         if(tel==arr[i].user){
            bol=false;
            res.send("该手机号已存在，请直接登录");
         }
      }
      if(bol){
          res.send("可以注册");
      }
   });
});
/*获取验证码*/
http.get("/getYzm",function (req,res) {
    var yzm=""+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9);
    res.send(yzm);
});
/*获取校验码*/
http.post("/getJYm",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var bol=true;
        var jym=""+parseInt(Math.random()*9)+parseInt(Math.random()*9)+parseInt(Math.random()*9)+
            parseInt(Math.random()*9)+parseInt(Math.random()*9);
        var dataObj=query.parse(dataStr);
        fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
            var arr=JSON.parse(data);
            for(var i=0;i<arr.length;i++){
                if(dataObj.tel==arr[i].tel){
                    arr[i].time=dataObj.time;
                    arr[i].jym=jym;
                    bol=false;
                    fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),function () {
                        res.send(jym);
                    });
                }
            }
            if(bol){
                dataObj.jym=jym;
                arr.push(dataObj);
                fs.writeFile("./src/data/xiaoyan.json",JSON.stringify(arr),function () {
                    res.send(jym);
                });
            }
        });
    });
});
/*注册用户*/
http.post("/addUser",function (req,res) {
   var dataStr="";
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
       var dataObj=query.parse(dataStr);
       fs.readFile("./src/data/xiaoyan.json","utf-8",function (err,data) {
           var arr=JSON.parse(data);
           for(var i=0;i<arr.length;i++){
               if(dataObj.tel==arr[i].tel){
                   if(dataObj.time-arr[i].time>15000){
                       res.send("0");
                   }else if(dataObj.jym==arr[i].jym){
                       fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
                          var arr1=JSON.parse(data);
                          var obj={
                              user:dataObj.tel,
                              id:dataObj.time,
                              pass:dataObj.pwd
                          };
                          arr1.push(obj);
                          fs.writeFile("./src/data/userlist.json",JSON.stringify(arr1),"utf-8",function () {
                              res.send("1");
                          });
                       });
                   }else {
                       res.send("-1");
                   }
               }
           }
       });
   });
});
/*登录*/
http.post("/login",function (req,res) {
   var dataStr="";
   var bol=true;
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
      fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
        var arr=JSON.parse(data);
        var dataObj=query.parse(dataStr);
        for(var i=0;i<arr.length;i++){
            if(dataObj.user==arr[i].user && dataObj.pass==arr[i].pass){
                bol=false;
                res.send("1");
            }
        }
        if(bol){
            res.send("0");
        }
      });
   });
});
/*获取用户ID*/
http.post("/oneUser",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
            var arr=JSON.parse(data);
            for(var i=0;i<arr.length;i++){
                if(dataObj.user==arr[i].user && dataObj.pass==arr[i].pass){
                    res.send(arr[i].id);
                }
            }
        });
    });
});
/*自动登陆*/
http.post("/autoLogin",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
        var dataObj=query.parse(dataStr);
        fs.readFile("./src/data/userlist.json","utf-8",function (err,data) {
            var arr=JSON.parse(data);
            for(var i=0;i<arr.length;i++){
                if(dataObj.id==arr[i].id){
                    res.send(arr[i].user);
                }
            }
        });
    });
});
/*注销用户*/
http.post("/logout",function (req,res) {
    var dataStr="";
    req.on("data",function (data) {
        dataStr+=data;
    });
    req.on("end",function () {
       res.send("注销成功");
    });
});
/*排序*/
http.get("/sort",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/shops.json",function (err,data) {
        var obj=JSON.parse(data);
        var arr=obj["shop_data"];
        var backArr=[];
        if(dataObj.type==""){
            res.send(arr);
            return;
        }
        var len=arr.length;
        for(var i=0;i<len;i++){
            var max=arr[0][dataObj.type];
            var index=0;
            for(var j=0;j<arr.length;j++){
                if(max*1<arr[j][dataObj.type]*1){
                    max=arr[j][dataObj.type];
                    index=j;
                }
            }
            backArr.push(arr[index]);
            arr.splice(index,1);
        }
        res.send(backArr);
    });
});
/*好评排序*/
http.get("/comments",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/shops.json",function (err,data) {
        var obj=JSON.parse(data);
        var arr=obj["shop_data"];
        var backArr=[];
        var len=arr.length;
        for(var i=0;i<len;i++){
            var max=arr[0]["comments"];
            var index=0;
            for(var j=0;j<arr.length;j++){
                if(max*1<arr[j]["comments"]*1){
                    max=arr[j]["comments"];
                    index=j;
                }
            }
            backArr.push(arr[index]);
            arr.splice(index,1);
        }
        res.send(backArr.slice(0,7));
    });
});
/*查找店铺*/
http.get("/findShop",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/shops.json","utf-8",function (err,data) {
        var arr=(JSON.parse(data))["shop_data"];
        for(var i=0;i<arr.length;i++){
            if(dataObj.id==arr[i]["qid"]){
                    res.send(arr[i]);
            }
        }
    });
});
/*进入店铺显示商品*/
http.get("/showGoods",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/"+dataObj.url,"utf-8",function (err,data) {
        var arr=(JSON.parse(data))["product"];
        for(var i=0;i<arr.length;i++){
            if(dataObj.id==arr[i]["product_id"]){
                res.send(arr[i]);
            }
        }
    });
});
/*添加到购物车*/
http.post("/addCar",function (req,res) {
   var dataStr="";
   var bol=true;
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
      fs.readFile("./src/data/shopcar.json","utf-8",function (err,data) {
          var arr=JSON.parse(data);
          var dataObj=query.parse(dataStr);
          for(var i=0;i<arr.length;i++){
             if(dataObj.id==arr[i].id){
                 arr[i].count=arr[i].count*1+1;
                 bol=false;
                 fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
                     res.send("ok");
                 });
             }
          }
          if(bol){
             arr.push(dataObj);
             fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
                 res.send("ok");
             });
          }
      });
   });
});
/*改变数量*/
http.post("/changeCount",function (req,res) {
   var dataStr="";
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
       fs.readFile("./src/data/shopcar.json","utf-8",function (err,data) {
           var arr=JSON.parse(data);
           var dataObj=query.parse(dataStr);
           for(var i=0;i<arr.length;i++){
               if(dataObj.id==arr[i].id){
                   if(dataObj.type=="+"){
                       arr[i].count=arr[i].count*1+1;
                   }else {
                       arr[i].count=arr[i].count*1-1;
                       if(arr[i].count<1){
                           arr[i].count=1;
                       }
                   }
                   fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
                       res.send("改变成功");
                   });
               }
           }
       })
   })
});
/*删除单个商品*/
http.post("/delOneGoods",function (req,res) {
   var dataStr="";
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
       fs.readFile("./src/data/shopcar.json","utf-8",function (err,data) {
          var dataObj=query.parse(dataStr);
          var arr=JSON.parse(data);
          for(var i=0;i<arr.length;i++){
              if(dataObj.id==arr[i].id){
                  arr.splice(i,1);
              }
          }
           fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
               res.send("删除成功");
           });
       });
   })
});
/*全选*/
http.post("/checkAll",function (req,res) {
   var dataStr="";
   req.on("data",function (data) {
       dataStr+=data;
   });
   req.on("end",function () {
      var dataObj=query.parse(dataStr);
      fs.readFile("./src/data/shopcar.json","utf-8",function (err,data) {
         var arr=JSON.parse(data);
         for(var i=0;i<arr.length;i++){
             arr[i].isChoose=dataObj.isChoose;
         }
          fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
              res.send("1");
          });
      });
   });
});
/*单选*/
http.get("/oneChoose",function (req,res) {
    var dataObj=req.query;
    fs.readFile("./src/data/shopcar.json","utf-8",function (err,data) {
       var arr=JSON.parse(data);
       for(var i=0;i<arr.length;i++){
           if(dataObj.id==arr[i].id){
               arr[i].isChoose=dataObj.isChoose;
           }
       }
        fs.writeFile("./src/data/shopcar.json",JSON.stringify(arr),"utf-8",function () {
            res.send("ok");
        });
    });
});
/*获取所有信息*/
http.all("*",function (req,res) {
   res.sendFile(__dirname+req.url);
});